<template>
  <base-page v-bind="baseInfo">
    <template #searchBar>
      <filter-form
        ref="filterForm"
        :show-reset="true"
        :elements="elements"
        @submit="submit"
      >
        <template>
          <Button :loading="downloading" icon="export" @click="download">导出</Button>
        </template>
      </filter-form>
    </template>
    <template #content>
      <div class="table-box">
        <TableNew
          :loading="tableLoading"
          :data="tableData"
          :columns="tableEl"
          :pagination="pagination"
          @paginationChange="paginationChange"
        />
      </div>
    </template>
  </base-page>
</template>

<script>
import baseFilterTable from '@/mixins/baseFilterTable'
import {
  getScoreList,
  importExcel
} from '@/api/collectionManage/scoreTable'
import {
  listYsDepartment
} from '@/api/public.js'

export default {
  name: 'ScoreTable', // 预算考核计分基础表
  components: {},
  mixins: [baseFilterTable],
  props: {},
  data() {
    return {
      baseInfo: {
        searchBarShow: true
      },
      tableData: [],
      tableEl: [
        {
          title: '部门',
          dataIndex: 'deptName',
          width: 110
        },
        {
          title: '全年预算',
          dataIndex: 'columnTw',
          type: 'hasMerge',
          align: 'center',
          mergeList: [
            {
              title: '合计',
              dataIndex: 'total',
              format: true,
              align: 'right',
              width: 110
            },
            {
              title: '业务职能',
              dataIndex: 'businessTotal',
              format: true,
              align: 'right',
              width: 110
            },
            {
              title: '管理职能',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '管理合计',
                  dataIndex: 'manageTotal',
                  format: true,
                  align: 'right',
                  width: 110
                },
                {
                  title: '费用类',
                  dataIndex: 'accountTotal',
                  format: true,
                  align: 'right',
                  width: 110
                },
                {
                  title: '资本类',
                  dataIndex: 'assetTotal',
                  format: true,
                  align: 'right',
                  width: 110
                },
                {
                  title: '药品',
                  dataIndex: 'drugTotal',
                  format: true,
                  align: 'right',
                  width: 110
                },
                {
                  title: '耗材',
                  dataIndex: 'consumablesTotal',
                  format: true,
                  align: 'right',
                  width: 110
                }
              ]
            }
          ]
        },
        {
          title: '占比',
          dataIndex: 'columnTh',
          type: 'hasMerge',
          align: 'center',
          mergeList: [
            {
              title: '实际占比',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '业务职能(%)',
                  dataIndex: 'businessRate',
                  format: true,
                  align: 'right',
                  width: 110
                },
                {
                  title: '管理职能(%)',
                  dataIndex: 'manageRate',
                  format: true,
                  align: 'right',
                  width: 110
                }
              ]
            },
            {
              title: '业务职能',
              dataIndex: 'businessRate2',
              format: true,
              align: 'right',
              width: 110
            },
            {
              title: '管理职能',
              type: 'hasMerge',
              mergeList: [
                {
                  title: '管理合计(%)',
                  dataIndex: 'manageRate2',
                  format: true,
                  align: 'right',
                  width: 110
                },
                {
                  title: '费用类(%)',
                  dataIndex: 'accountRate',
                  format: true,
                  align: 'right',
                  width: 110
                },
                {
                  title: '资本类(%)',
                  dataIndex: 'assetRate',
                  format: true,
                  align: 'right',
                  width: 110
                },
                {
                  title: '药占比(%)',
                  dataIndex: 'drugRate',
                  format: true,
                  align: 'right'
                },
                {
                  title: '耗占比(%)',
                  dataIndex: 'consumablesRate',
                  format: true,
                  align: 'right',
                  width: 110
                }
              ]
            }
          ]
        }
      ],
      elements: [
        {
          label: '时间',
          type: 'date-picker',
          mode: 'monthrange',
          key: 'time',
          value: [],
          format: 'yyyyMM',
          choiceDate: '',
          pickerOptions: this.$pickerOptions()
        },
        {
          label: '部门',
          type: 'select',
          options: [],
          key: 'deptName',
          value: ''
        }
      ],
      downloading: false
    }
  },
  created() {
    this._listYsDepartment()
    this.initMonth()
  },
  mounted() {},
  methods: {
    async _listYsDepartment() {
      try {
        const { data } = await listYsDepartment({ status: 0 })
        if (data.code === 200 && data.data?.length) {
          this.elements.find(i => i.key === 'deptName').options.push(...data.data.map(j => ({ label: j.departmentName, value: j.departmentName })))
        } else {
          this.$message.error(data.message || '部门获取失败')
        }
      } catch (error) {
        console.log(error)
      }
    },
    initMonth() {
      // const year = new Date().getFullYear()
      // const month = new Date().getMonth() + 1
      // const now = year + (month + '').padStart(2, '0')
      // this.elements.find(i => i.type === 'date-picker' && i.mode === 'monthrange').value.push(now, now)
      const { startTime, endTime } = this.$getQuarter()
      this.elements.find(i => i.type === 'date-picker' && i.mode === 'monthrange').value.push(startTime, endTime)
    },
    async getData(val) {
      this.tableLoading = true
      const params = {
        startTime: val?.time?.length ? val.time[0] : '',
        endTime: val?.time?.length ? val.time[1] : '',
        deptName: val?.deptName || '',
        pageNum: this.pagination.current,
        pageSize: this.pagination.pageSize
      }
      try {
        const { data } = await getScoreList(params)
        if (data.code === 200) {
          this.tableData = data.data.list
          this.pagination.total = data.data.total
        } else {
          console.log(data)
        }
      } catch (error) {
        console.log(error)
      } finally {
        this.tableLoading = false
      }
    },
    async download() {
      const val = this.$refs.filterForm.getParms()
      const params = {
        startTime: val?.time?.length ? val.time[0] : '',
        endTime: val?.time?.length ? val.time[1] : '',
        deptName: val?.deptName || ''
      }
      this.downloading = true
      try {
        const res = await importExcel(params)
        if (res.data.type === 'application/json') {
          this.$message.error('下载失败')
        } else {
          const blob = new Blob([res.data])
          const fileName = `${this.type === '1' ? '业务职能预算编制数据' : '管理职能预算编制数据'}` + '.xlsx'
          const URL = window.URL || window.webkitURL || window.moxURL
          if ('download' in document.createElement('a')) {
          // 非IE下载
            const elink = document.createElement('a')
            elink.download = fileName // 命名下载名称
            elink.style.display = 'none'
            elink.href = URL.createObjectURL(blob) // 表示一个指定的file对象或Blob对象
            document.body.appendChild(elink)
            elink.click() // 点击触发下载
            URL.revokeObjectURL(elink.href) // 释放URL 对象
            document.body.removeChild(elink)
          } else {
          // IE10+下载
            navigator.msSaveBlob(blob, fileName)
          }
        }
      } catch (error) {
        console.log(error)
      } finally {
        this.downloading = false
      }
    }
  }
}
</script>

<style lang="less" scoped>
.table-box{
  height: 100%;
  display: flex;
  flex-direction: column;
}
</style>
